﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication9.Models;

namespace MvcApplication9.Controllers
{ 
    public class ExcursionController : Controller
    {
        private MainModelContainer db = new MainModelContainer();


        static int? Id;
        //
        // GET: /Excursion/

        public ViewResult Index(int? point_id)
        {
            List<Excursion> exc = null;
            if (point_id != null)
            {
                Id = point_id;
            }
            exc = (from i in db.Excursions where i.CheckPoint.Id == Id select i).ToList();
            return View(exc);
        }

        //
        // GET: /Excursion/Details/5

        public ViewResult Details(int id)
        {
            Excursion excursion = db.Excursions.Single(e => e.Id == id);
            return View(excursion);
        }

        //
        // GET: /Excursion/Create

        public ActionResult Create()
        {
            return RedirectToAction("AddExcursion", "CheckPoint", new { exc_id = Id });
        } 

        //
        // POST: /Excursion/Create

        [HttpPost]
        public ActionResult Create(Excursion excursion)
        {
            if (ModelState.IsValid)
            {
                db.Excursions.AddObject(excursion);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(excursion);
        }
        
        //
        // GET: /Excursion/Edit/5
 
        public ActionResult Edit(int id)
        {
            Excursion excursion = db.Excursions.Single(e => e.Id == id);
            return View(excursion);
        }

        //
        // POST: /Excursion/Edit/5

        [HttpPost]
        public ActionResult Edit(Excursion excursion)
        {
            if (ModelState.IsValid)
            {
                db.Excursions.Attach(excursion);
                db.ObjectStateManager.ChangeObjectState(excursion, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(excursion);
        }

        //
        // GET: /Excursion/Delete/5
 
        public ActionResult Delete(int id)
        {
            Excursion excursion = db.Excursions.Single(e => e.Id == id);
            return View(excursion);
        }

        //
        // POST: /Excursion/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Excursion excursion = db.Excursions.Single(e => e.Id == id);
            db.Excursions.DeleteObject(excursion);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}